package com.cunshe.service;

import com.cunshe.dao.MessageBoardDao;
import com.cunshe.domain.MessageBoard;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sunkee.entity.page.Pageination;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class MessageBoardService {

    @Autowired
    private MessageBoardDao messageBoardDao;

    @Transactional(rollbackFor = Exception.class)
    public int insert(MessageBoard record) {
        record.setCreatetime(new Date());
        return messageBoardDao.insert(record);
    }

    @Transactional(rollbackFor = Exception.class)
    public int delete(Long id) {
        return messageBoardDao.delete(id);
    }

    @Transactional(rollbackFor = Exception.class)
    public int update(MessageBoard record) {
        return messageBoardDao.update(record);
    }

    public Pageination<MessageBoard> pagelist(int pageNo, int pageSize, MessageBoard record) {
        Page<MessageBoard> page = PageHelper.startPage(pageNo, pageSize, " createtime DESC ");
        List<MessageBoard> list = messageBoardDao.list(record);
        Pageination<MessageBoard> of = Pageination.of(pageNo, pageSize);
        of.setResult(list).setCount(page.getTotal());
        return of;
    }

    public List<MessageBoard> list(MessageBoard record) {
        return messageBoardDao.list(record);
    }

}